package com.mlz.mlzmall.ui.activity.act.personal;

import android.content.Intent;
import android.content.pm.ActivityInfo;
import android.os.Bundle;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.android.volley.Request;
import com.mlz.mlzmall.R;
import com.mlz.mlzmall.bean.MyOrderList;
import com.mlz.mlzmall.ui.activity.act.base.NetworkActivity;
import com.mlz.mlzmall.ui.activity.act.base.TopTitleView;
import com.mlz.mlzmall.ui.activity.act.personal.NetworkRequest.VolleyBaseActivity;
import com.mlz.mlzmall.ui.adapter.MyAdapterAll;
import com.mlz.mlzmall.ui.inter.NetResponses;
import com.mlz.mlzmall.ui.utils.mlz.ConfigInfo;
import com.mlz.mlzmall.ui.utils.mlz.DialogView;
import com.mlz.mlzmall.ui.utils.mlz.URL;
import com.mlz.mlzmall.volley.util.JsonUtils;

import org.json.JSONObject;

import java.text.DecimalFormat;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;


public class MyOrder extends NetworkActivity implements NetResponses {
    private TextView qbTitle;// 全部
    private TextView payTitle;// 待付款
    private TextView getTitle;// 待收货
    private TextView sayTitle;// 待评价
    ListView lv_order1;
    RelativeLayout rl_myOrder_null;
    private LinearLayout ll_cache;//当有数据的时候就显示订单信息
    DecimalFormat df;
    String TYPE;

    private Map map = new HashMap();

    ArrayList<MyOrderList.Data.LIST.Product> allOrder = new ArrayList();
    ArrayList<MyOrderList.Data.LIST.Product> doneOrder = new ArrayList();
    ArrayList<MyOrderList.Data.LIST.Product> payOrder = new ArrayList();
    ArrayList<MyOrderList.Data.LIST.Product> deliveryOrder = new ArrayList();

    ArrayList<MyOrderList.Data.LIST> orders;

    MyAdapterAll myAdapterAll, myAdapterPAY, myAdapterSAY, myAdapterGET;


    String tag;

    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.my_order_activity);
        setNetResponses(this);
        tag = "MyOrder";
        setViewContent();
        this.setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
        init();
        //全部订单
        TYPE = "ALL";
        initData();

}
    private void setViewContent() {
        TopTitleView loginTopTitle = (TopTitleView) findViewById(R.id.login_top_title);
        loginTopTitle.setLeftButtonContent(null, R.mipmap.pic_2_1_btn_back, null);
        loginTopTitle.setTitleButtonContent("我的订单", null, null);
        loginTopTitle.setOnLeftButtonClickListener(new TopTitleView.OnLeftButtonClickListener() {
            @Override
            public void onLeftBtnClick(View button) {
                finish();
                MyOrder.this.overridePendingTransition(R.anim.right_in,R.anim.right_out);
            }
        });
    }
    @Override
    protected void onStart() {
        super.onStart();
//        //全部订单
//        TYPE = "ALL";
//        initData();

    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (requestCode == 100 && resultCode == 200) {
            initData();
        }
    }

    private void init() {
        df = new DecimalFormat("0.00 ");//使价格后保留两位小数
        qbTitle = (TextView) findViewById(R.id.tv_everything);
        payTitle = (TextView) findViewById(R.id.tv_pay_will);
        getTitle = (TextView) findViewById(R.id.tv_get_goods_will);
        sayTitle = (TextView) findViewById(R.id.tv_say_will);
        qbTitle.setOnClickListener(this);
        payTitle.setOnClickListener(this);
        getTitle.setOnClickListener(this);
        sayTitle.setOnClickListener(this);
        ll_cache = (LinearLayout) findViewById(R.id.ll_cache);//当有数据的时候就显示订单信息
        lv_order1 = (ListView) findViewById(R.id.lv_list_order1);
        rl_myOrder_null = (RelativeLayout) findViewById(R.id.rl_myOrder_null);
    }

    private void initData() {
        request(Request.Method.GET, URL.ORDER.LIST, null, sessionId, tag, 100);
    }


    //实现标题的点击和下面页面的同步切换
    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.tv_everything:
                setBottomSelected(R.id.iv_order1);
                System.out.println("@@@==" + allcount);
                if (allcount == 0) {
                    rl_myOrder_null.setVisibility(View.VISIBLE);
                    lv_order1.setVisibility(View.GONE);
                    break;
                } else {
                    lv_order1.setVisibility(View.VISIBLE);
                    rl_myOrder_null.setVisibility(View.GONE);
                }
                lv_order1.setAdapter(myAdapterAll);
                myAdapterAll.notifyDataSetChanged();
                break;

            case R.id.tv_pay_will:
                setBottomSelected(R.id.iv_order2);
                if (paycount == 0) {
                    lv_order1.setVisibility(View.GONE);
                    rl_myOrder_null.setVisibility(View.VISIBLE);
                    break;
                } else {
                    rl_myOrder_null.setVisibility(View.GONE);
                    lv_order1.setVisibility(View.VISIBLE);
                }
                lv_order1.setAdapter(myAdapterPAY);
                myAdapterPAY.notifyDataSetChanged();
                break;

            case R.id.tv_get_goods_will:
                setBottomSelected(R.id.iv_order3);
                if (getcount == 0) {
                    rl_myOrder_null.setVisibility(View.VISIBLE);
                    lv_order1.setVisibility(View.GONE);
                    break;
                } else {
                    rl_myOrder_null.setVisibility(View.GONE);
                    lv_order1.setVisibility(View.VISIBLE);
                }
                lv_order1.setAdapter(myAdapterGET);
                myAdapterGET.notifyDataSetChanged();
                break;

            case R.id.tv_say_will:
                setBottomSelected(R.id.iv_order4);
                if (saycount == 0) {
                    rl_myOrder_null.setVisibility(View.VISIBLE);
                    lv_order1.setVisibility(View.GONE);
                    break;
                } else {
                    rl_myOrder_null.setVisibility(View.GONE);
                    lv_order1.setVisibility(View.VISIBLE);
                }
                lv_order1.setAdapter(myAdapterSAY);
                myAdapterSAY.notifyDataSetChanged();
                break;

            default:
                break;
        }
    }

    private void setBottomSelected(int selectedId) {
        findViewById(R.id.iv_order1).setSelected(false);
        findViewById(R.id.iv_order2).setSelected(false);
        findViewById(R.id.iv_order3).setSelected(false);
        findViewById(R.id.iv_order4).setSelected(false);
        findViewById(selectedId).setSelected(true);
    }


    int allcount, paycount, getcount, saycount;

    @Override
    public void success(int type, JSONObject jsonObject) {

        switch (type) {
            case 100:
                MyOrderList myOrder = JsonUtils.object(jsonObject.toString(), MyOrderList.class);
                orders = (ArrayList<MyOrderList.Data.LIST>) myOrder.getData().getList();
                allOrder.clear();
                payOrder.clear();
                doneOrder.clear();
                deliveryOrder.clear();
                allcount = 0;
                paycount = 0;
                getcount = 0;
                saycount = 0;
                for (int i = 0; i < orders.size(); i++) {
                    int start = 1;
                    int len = orders.get(i).getProducts().size();
                    if ("waiting_for_payment".equals(orders.get(i).getStatusCode())) {
                        for (int j = 0; j < orders.get(i).getProducts().size(); j++) {
                            orders.get(i).getProducts().get(j).setOrderCoderForgroup(orders.get(i).getOrderCode());

                            if (j == 0) {
                                orders.get(i).getProducts().get(j).setIsFirst(true);
                            } else {
                                orders.get(i).getProducts().get(j).setIsFirst(false);
                            }
                            //用于标记最后一个产品
                            if (start == len) {
                                orders.get(i).getProducts().get(j).setIsLast(true);
                            } else {
                                orders.get(i).getProducts().get(j).setIsLast(false);
                            }
                            payOrder.add(orders.get(i).getProducts().get(j));
                            allOrder.add(orders.get(i).getProducts().get(j));
                            start++;
                        }
                        paycount++;
                        allcount++;
                    } else if ("waiting_for_delivery".equals(orders.get(i).getStatusCode())
                            || "delivered".equals(orders.get(i).getStatusCode())) {
                        for (int j = 0; j < orders.get(i).getProducts().size(); j++) {
                            orders.get(i).getProducts().get(j).setOrderCoderForgroup(orders.get(i).getOrderCode());

                            if (j == 0) {
                                orders.get(i).getProducts().get(j).setIsFirst(true);
                            } else {
                                orders.get(i).getProducts().get(j).setIsFirst(false);
                            }
                            //用于标记最后一个产品
                            if (start == len) {
                                orders.get(i).getProducts().get(j).setIsLast(true);
                            } else {
                                orders.get(i).getProducts().get(j).setIsLast(false);
                            }

                            deliveryOrder.add(orders.get(i).getProducts().get(j));
                            allOrder.add(orders.get(i).getProducts().get(j));
                            start++;
                        }
                        getcount++;
                        allcount++;
                    } else if ("done".equals(orders.get(i).getStatusCode())) {
                        for (int j = 0; j < orders.get(i).getProducts().size(); j++) {
                            orders.get(i).getProducts().get(j).setOrderCoderForgroup(orders.get(i).getOrderCode());
                            if (j == 0) {
                                orders.get(i).getProducts().get(j).setIsFirst(true);
                            } else {
                                orders.get(i).getProducts().get(j).setIsFirst(false);
                            }
                            //用于标记最后一个产品
                            if (start == len) {
                                orders.get(i).getProducts().get(j).setIsLast(true);
                            } else {
                                orders.get(i).getProducts().get(j).setIsLast(false);
                            }

                            doneOrder.add(orders.get(i).getProducts().get(j));
                            allOrder.add(orders.get(i).getProducts().get(j));
                            start++;
                        }
                        saycount++;
                        allcount++;
                    } else {
                        for (int j = 0; j < orders.get(i).getProducts().size(); j++) {
                            orders.get(i).getProducts().get(j).setOrderCoderForgroup(orders.get(i).getOrderCode());
                            if (j == 0) {
                                orders.get(i).getProducts().get(j).setIsFirst(true);
                            } else {
                                orders.get(i).getProducts().get(j).setIsFirst(false);
                            }
                            //用于标记最后一个产品
                            if (start == len) {
                                orders.get(i).getProducts().get(j).setIsLast(true);
                            } else {
                                orders.get(i).getProducts().get(j).setIsLast(false);
                            }
                            allOrder.add(orders.get(i).getProducts().get(j));
                            start++;

                        }
                        allcount++;
                    }
                }

                qbTitle.setText("全部" + "(" + String.valueOf(allcount) + ")");
                sayTitle.setText("待评论" + "(" + String.valueOf(saycount) + ")");
                getTitle.setText("待收货" + "(" + String.valueOf(getcount) + ")");
                payTitle.setText("待付款" + "(" + String.valueOf(paycount) + ")");

                if ("ALL".equals(TYPE)) {
                    myAdapterAll = new MyAdapterAll(this, R.layout.order1_item, orders, allOrder);
                    myAdapterPAY = new MyAdapterAll(this, R.layout.order1_item, orders, payOrder);
                    myAdapterSAY = new MyAdapterAll(this, R.layout.order1_item, orders, doneOrder);
                    myAdapterGET = new MyAdapterAll(this, R.layout.order1_item, orders, deliveryOrder);
                    setBottomSelected(R.id.iv_order1);
                    lv_order1.setAdapter(myAdapterAll);
                } else if ("PAY".equals(TYPE)) {
                    myAdapterPAY.notifyDataSetChanged();
                } else if ("SAY".equals(TYPE)) {
                    myAdapterSAY.notifyDataSetChanged();
                    myAdapterGET.notifyDataSetChanged();
                }
                break;
            case 200:
                if (jsonObject.optInt("status") == 1303) {
                    ResultPopWindow("商家未发货，请查看物流!");
                } else if (0 == jsonObject.optInt("status")) {
                    ResultPopWindow("收货成功！");
//                    TYPE = "SAY";
                    initData();
                }
                break;
            case 300:
//                TYPE = "PAY";
                initData();
                break;
        }


    }

    @Override
    public void fail() {

    }

    // 取消订单
    public void cancelOrder(final String orderCode) {
        map.clear();
        map.put("orderCode", orderCode);
        map.put("reason", "string");
        request(Request.Method.POST, URL.ORDER.CANCEL, map, sessionId, tag, 300);
    }

    // 确认收货
    public void confirmRecipt(final String orderCode) {
        map.clear();
        map.put("orderCode", orderCode);
        map.put("reason", "string");
        request(Request.Method.POST, URL.ORDER.DELIVER, map, sessionId, tag, 200);
    }

    // 确认收货对话框
    public void ResultPopWindow(String message) {
        DialogView dialogView = new DialogView(this, R.layout.dialogview_one, R.style.pager_dialog, 1, message);
        dialogView.show();
    }
}
